package com.nian.app.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.nian.app.api.AppSwiperImagesAPI;
import com.nian.app.model.AppSwiperImages;
import com.nian.common.model.SerializeUtils;
import com.nian.common.model.ServerResponse;
import com.nian.redis.cache.JedisClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用途：
 * 开发：zhangmj
 * 日期：2020/4/2 0002 9:39
 */
@RestController
@RequestMapping("swiper")
public class AppSwiperImagesController {

    @Autowired
    private AppSwiperImagesAPI client;
    @Autowired
    private JedisClient jedisClient;
    /**
     *
     * @param terminalType
     * @param pageType
     * @return
     */
    @GetMapping("list/{terminalType}/{pageType}")
    @HystrixCommand
    public ServerResponse<List<AppSwiperImages>> list(@PathVariable("terminalType") Integer terminalType, @PathVariable("pageType") Integer pageType){
        // 1 取缓存, 如果缓存没有，取数据库，在写入缓存
        String key = "app_controller_swiper_list" + terminalType + "_" + pageType;
        String value = jedisClient.get(key);
        if(StringUtils.isNotEmpty(value)){
            List<AppSwiperImages> list = (List<AppSwiperImages>) SerializeUtils.serializeToObject(value);
            return ServerResponse.createBySuccess(list);
        }
        ServerResponse<List<AppSwiperImages>> response = client.swiperList(terminalType, pageType);
        if(response.isSuccess() && !CollectionUtils.isEmpty(response.getData())){
            jedisClient.set(key, SerializeUtils.serialize(response.getData()));
        }
        return response;
    }

    @GetMapping("list1")
    public ServerResponse<List<AppSwiperImages>> list(){
        // 1 取缓存, 如果缓存没有，取数据库，在写入缓存
        return client.swiperList(2, 1);
    }

}
